from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall.settings import SECRET_KEY


# itsdangerous使用(加密)
"""
itsdangerous就是为了数据加密
# 1. 导入itsdangerous 的类
# 2. 创建类的实例对象
# 3. 数据加密
"""


# # 1. 导入itsdangerous 的类
# from itsdangerous import TimedJSONWebSignatureSerializer as Serializer  # 数据加密和时效
# # 2. 创建类的实例对象
# # secret_key, expires_in=None,
# s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
# # 3. 数据加密
# token = s.dumps({'openid': "1234567890"})
# # 4.解密
# s.loads(token)


# 加密
def generic_openid(add_secret_data):

    s = Serializer(secret_key=SECRET_KEY, expires_in=3600)
    access_token = s.dumps(add_secret_data)

    # 将bytes类型转化为str
    return access_token.decode()


# 解密
def check_access_token(loads_secret_data):
    s = Serializer(secret_key=SECRET_KEY, expires_in=3600)
    try:
        results = s.loads(loads_secret_data)
    except Exception:
        return None
    else:
        return results



